package edu.ar.modulo4.ejb.cliente;

import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Connection;
import javax.jms.Session;
import javax.jms.MessageProducer;
import javax.jms.TextMessage;
import javax.jms.JMSException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class ConverterClienteEJB {

	private ConnectionFactory connectionFactory;
	private Queue queue;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		ConverterClienteEJB test = new ConverterClienteEJB();
		test.inicializar();
		test.runTheTest();
	}

	private void inicializar() {

		Context initialContext;
		try {
			
			initialContext = new InitialContext();
			connectionFactory = (ConnectionFactory) initialContext.lookup("jms/ConnectionFactory");
			queue = (Queue) initialContext.lookup("jms/Queue");
			
		} catch (NamingException e) {
			
			e.printStackTrace();
		}
		
	}

	private void runTheTest() {

		Connection connection = null;
		Session session = null;
		MessageProducer messageProducer = null;
		TextMessage message = null;
		
		try {
			
			connection = connectionFactory.createConnection();
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			messageProducer = session.createProducer(queue);
			message = session.createTextMessage();
			
			message.setText("1");
			System.out.println("Valor enviando: " + message.getText());
			messageProducer.send(message);			
			
		} catch (JMSException e) {
			System.out.println("Exception occurred: " + e.toString());
			e.printStackTrace();
			
		} finally {
			
			if (connection != null) {
				try {
					connection.close();
				} catch (JMSException e) {
					System.out.println("Exception occurred: " + e.toString());
					e.printStackTrace();
				}
			}
			
			System.exit(0);
		}

	}
}
